﻿
using System.Windows.Controls;
using UBCon.Ribbon.Interfaces;
using System.Windows;
using System;

namespace UBCon.Ribbon
{
    internal class TwoLineText:Control, IRibbonControl
    {
        //------------------------------------------------------
        //
        //  Constructors
        //
        //------------------------------------------------------
        #region Constructors
        
        /// <summary>
        /// Default static constructor.
        /// </summary>
        static TwoLineText()
        {
            Type ownerType = typeof(TwoLineText);
            DefaultStyleKeyProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(ownerType));
        }
       
        #endregion

        //------------------------------------------------------
        //
        //  Properties
        //
        //------------------------------------------------------
        #region Propertie

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        internal static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(TwoLineText), new FrameworkPropertyMetadata(
                (string)null, 
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, 
                new PropertyChangedCallback(OnTextPropertyChanged)));

        public string Text1
        {
            get { return (string)GetValue(Text1Property); }
            set { SetValue(Text1Property, value); }
        }

        // Using a DependencyProperty as the backing store for Text1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Text1Property =
            DependencyProperty.Register("Text1", typeof(string), typeof(TwoLineText), new FrameworkPropertyMetadata(
                (string)null,
                FrameworkPropertyMetadataOptions.AffectsParentMeasure));

        public string Text2
        {
            get { return (string)GetValue(Text2Property); }
            set { SetValue(Text2Property, value); }
        }

        // Using a DependencyProperty as the backing store for Text2.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Text2Property =
            DependencyProperty.Register("Text2", typeof(string), typeof(TwoLineText), new System.Windows.FrameworkPropertyMetadata((string)null,
                FrameworkPropertyMetadataOptions.AffectsParentMeasure));

        public System.Windows.Visibility ImageVisibility
        {
            get { return (System.Windows.Visibility)GetValue(ImageVisibilityProperty); }
            set { SetValue(ImageVisibilityProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImageVisibility.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageVisibilityProperty =
            DependencyProperty.Register("ImageVisibility", typeof(System.Windows.Visibility), typeof(TwoLineText), new 
                PropertyMetadata(System.Windows.Visibility.Collapsed));

        #endregion

        //------------------------------------------------------
        //
        //  Depenency Property Changed Callbacks
        //
        //------------------------------------------------------
        #region Depenency Property Changed Callbacks

        /// <summary>
        /// Invoked when Text property changes.
        /// </summary>
        /// <param name="sender">Type: System.Windows.DependencyObject. The DependencyObject on which the property has changed value.</param>
        /// <param name="e">Type: System.Windows.DependencyPropertyChangedEventArgs. Event data that is issued by any event that tracks changes to the effective value of this property.</param>
        private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
        {
            
            string text = (string)e.NewValue;
            TwoLineText twoLine = (TwoLineText)d;
            if (!String.IsNullOrEmpty(text))
            {
                twoLine.SetText();
            }
            else 
            {
                twoLine.Text1 = System.String.Empty;
                twoLine.Text2 = System.String.Empty;
            }

        }
        #endregion

        #region Overrides

        /// <summary>
        /// Invoked whenever application code or internal processes (such as a rebuilding layout pass) call ApplyTemplate. 
        /// In simplest terms, this means the method is called just before a UI element displays in  app.  
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            TextBlock1 = GetTemplateChild("PART_TwoLineText1") as TextBlock;
            TextBlock2 = GetTemplateChild("PART_TwoLineText2") as TextBlock;
        }

        #endregion

        #region Private Utilities

        private void SetText() 
        {
            if (Text.Contains(@"\n"))
            {
                int index = Text.IndexOf(@"\n");
                string text1 = Text.Substring(0, index);
                string text2 = Text.Remove(0, text1.Length + 2);
                text1 = text1.Trim();
                text2 = text2.Trim();
                if (!String.IsNullOrEmpty(text1) || !String.IsNullOrEmpty(text2))
                {
                    if (String.IsNullOrEmpty(text1) && !String.IsNullOrEmpty(text2))
                    {
                        Text1 = text2;
                    }
                    else if (!String.IsNullOrEmpty(text1))
                    {
                        Text1 = text1;
                        Text2 = !String.IsNullOrEmpty(text2) ? text2 : null;
                    }
                }
            }
            else 
            {
                Text1 = Text;
            }
        }

        private void SetMargin() 
        {
            if (TextBlock1 != null && TextBlock2 != null) 
            {
                double largeScale = TextBlock1.DesiredSize.Width > TextBlock2.DesiredSize.Width ? TextBlock1.DesiredSize.Width : this.TextBlock2.DesiredSize.Width;
            }
        }
        #endregion

        #region Event Handlers

        #endregion

        #region Fields
        private TextBlock TextBlock1, TextBlock2;
        
        #endregion

        #region Events
        #endregion

        #region IRibbonControl Implementation

        /// <summary>
        /// Gets a boolean value indicating whether the contol's size is large
        /// </summary>
        public bool IsLarge
        {
            get { return false; }
        }

        #endregion
    }
}
